package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.CategorieRadio;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.LocalRadio;
import com.west_traveler.entity.entity_live_page.entiy_live_first_page.TopRadio;
import com.west_traveler.utils.CommonUtils;
import com.west_traveler.ximalayafm.MainActivity;
import com.west_traveler.ximalayafm.NormalLiveSecondPageActivity;
import com.west_traveler.ximalayafm.ProvinceRadioActivity;
import com.west_traveler.ximalayafm.R;

import java.util.List;

/**
 * created by Wang_JunJie at 2016/12/3
 */
public class LiveFragmentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
   private final int TYPE_FIRST=0;
   private final int TYPE_OTHERS_01=1;
   private final int TYPE_OTHERS_02=2;
   private final int TYPE_OTHERS_03=3;
    private Context context;
    private List<CategorieRadio> categories;
    private List<LocalRadio> localRadios;
    private String location;
    private List<TopRadio> topRadios;
    private MainActivity activity;

    public LiveFragmentRecyclerViewAdapter(Context context, List<CategorieRadio> categories, List<LocalRadio> localRadios, String location, List<TopRadio> topRadios,MainActivity activity) {
        this.context = context;
        this.categories = categories;
        this.localRadios = localRadios;
        this.location = location;
        this.topRadios = topRadios;
        this.activity=activity;
    }

    //判断布局的类型
    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return TYPE_FIRST;
        }else if(position==1){
            return TYPE_OTHERS_01;
        }else if(position==2){
            return TYPE_OTHERS_02;
        }else if(position==3){
            return TYPE_OTHERS_03;
        }else {
            return -1;
        }

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view=null;
        //根据不同的type 返回不同的布局
        switch (viewType) {
            case TYPE_FIRST:
                view= LayoutInflater.from(context).inflate(R.layout.fragment_live_item_first,parent,false);
                ViewHolderFirst holderFirst = new ViewHolderFirst(view);
                return holderFirst;
            case TYPE_OTHERS_01:
            case TYPE_OTHERS_02:
            case TYPE_OTHERS_03:
                view=LayoutInflater.from(context).inflate(R.layout.fragment_live_item_others,parent,false);
                ViewHolderOthers holderOthers = new ViewHolderOthers(view);
                return holderOthers;
        }
        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_FIRST:
                if (holder instanceof ViewHolderFirst) {



                    //给gridLayout添加子控件
                    final int radioCount = categories.size();
                    final GridLayout gridLayoutRadios=((ViewHolderFirst) holder).gridLayoutRadios;
                    if (radioCount!=0) {
                        gridLayoutRadios.setRowCount(4);
                        gridLayoutRadios.setColumnCount((radioCount/4)+1);
                        for (int i = 0; i < 7; i++) {
                            Button btn = new Button(context);
                            //给每一个按钮添加监听
                            //btn.setOnClickListener(this);
                            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                            layoutParams.width = CommonUtils.getWindowWidth(context)/4;
                            btn.setLayoutParams(layoutParams);
                            btn.setText(categories.get(i).getName());
                            gridLayoutRadios.addView(btn);
                        }
                        final Button btnContrl = new Button(context);
                        btnContrl.setBackgroundResource(R.mipmap.ic_triangle_down);
                        btnContrl.setTag("STATE_DOWN");
                        gridLayoutRadios.addView(btnContrl);


                        //给btnContrl设置点击监听
                        btnContrl.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //根据给btnContrl设置的tag来判断展开还是收起
                                if(btnContrl.getTag()=="STATE_DOWN"){
                                    for (int i = 7; i < radioCount; i++) {
                                        Button btn = new Button(context);
                                       // btn.setOnClickListener(this);
                                        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                                        layoutParams.width = CommonUtils.getWindowWidth(context)/4;
                                        btn.setLayoutParams(layoutParams);
                                        btn.setText(categories.get(i).getName());
                                        gridLayoutRadios.addView(btn,i);
                                        btnContrl.setBackgroundResource(R.mipmap.ic_triangle_up);
                                        btnContrl.setTag("STATE_UP");
                                    }
                                }else if(btnContrl.getTag()=="STATE_UP"){
                                    for (int i = 7; i < radioCount; i++) {
                                        gridLayoutRadios.removeView(gridLayoutRadios.getChildAt(7));
                                        btnContrl.setBackgroundResource(R.mipmap.ic_triangle_down);
                                        btnContrl.setTag("STATE_DOWN");
                                    }
                                }

                            }
                        });


                    }
                    //给每个控件添加点击监听
                    ((ViewHolderFirst) holder).localRadio.setOnClickListener(this);
                    ((ViewHolderFirst) holder).localRadio.setTag("localRadio");
                    ((ViewHolderFirst) holder).countryRadio.setOnClickListener(this);
                    ((ViewHolderFirst) holder).countryRadio.setTag("countryRadio");
                    ((ViewHolderFirst) holder).internetRadio.setOnClickListener(this);
                    ((ViewHolderFirst) holder).internetRadio.setTag("internetRadio");
                    //省市台的监听单独写 因为跳转界面不一样
                    ((ViewHolderFirst) holder).provinceRadio.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, ProvinceRadioActivity.class);
                            context.startActivity(intent);
                            activity.overridePendingTransition(R.anim.push_left_in,android.R.anim.fade_out);
                        }
                    });

                }
            break;
            case TYPE_OTHERS_01:
                if (holder instanceof ViewHolderOthers) {

                    TextView tv_live_peritem_head = ((ViewHolderOthers) holder).tv_live_peritem_head;
                    tv_live_peritem_head.setText(location);

                    LocalRadio localRadio_01 = localRadios.get(0);
                    Picasso.with(context).load(localRadio_01.getCoverLarge()).into(((ViewHolderOthers) holder).iv_peritem_01);
                    ((ViewHolderOthers) holder).tv_item_01_radioname.setText(localRadio_01.getName());
                    ((ViewHolderOthers) holder).tv_item_01_programname_id.setText(localRadio_01.getProgramName());
                    ((ViewHolderOthers) holder).tv_item_01_playcount_id.setText(localRadio_01.getPlayCount()+"");

                    LocalRadio localRadio_02 = localRadios.get(1);
                    Picasso.with(context).load(localRadio_02.getCoverLarge()).into(((ViewHolderOthers) holder).iv_peritem_02);
                    ((ViewHolderOthers) holder).tv_item_02_radioname.setText(localRadio_02.getName());
                    ((ViewHolderOthers) holder).tv_item_02_programname_id.setText(localRadio_02.getProgramName());
                    ((ViewHolderOthers) holder).tv_item_02_playcount_id.setText(localRadio_02.getPlayCount()+"");

                    LocalRadio localRadio_03 = localRadios.get(2);
                    Picasso.with(context).load(localRadio_03.getCoverLarge()).into(((ViewHolderOthers) holder).iv_peritem_03);
                    ((ViewHolderOthers) holder).tv_item_03_radioname.setText(localRadio_03.getName());
                    ((ViewHolderOthers) holder).tv_item_03_programname_id.setText(localRadio_03.getProgramName());
                    ((ViewHolderOthers) holder).tv_item_03_playcount_id.setText(localRadio_03.getPlayCount()+"");


                }
            break;
            case TYPE_OTHERS_02:
                if (holder instanceof ViewHolderOthers) {
                    TextView tv_live_peritem_head = ((ViewHolderOthers) holder).tv_live_peritem_head;
                    tv_live_peritem_head.setText("排行榜");

                    TopRadio topRadios_01 = topRadios.get(0);
                    Picasso.with(context).load(topRadios_01.getCoverLarge()).into(((ViewHolderOthers) holder).iv_peritem_01);
                    ((ViewHolderOthers) holder).tv_item_01_radioname.setText(topRadios_01.getName());
                    ((ViewHolderOthers) holder).tv_item_01_programname_id.setText(topRadios_01.getProgramName());
                    ((ViewHolderOthers) holder).tv_item_01_playcount_id.setText(topRadios_01.getPlayCount()+"");

                    TopRadio topRadios_02 = topRadios.get(1);
                    Picasso.with(context).load(topRadios_02.getCoverLarge()).into(((ViewHolderOthers) holder).iv_peritem_02);
                    ((ViewHolderOthers) holder).tv_item_02_radioname.setText(topRadios_02.getName());
                    ((ViewHolderOthers) holder).tv_item_02_programname_id.setText(topRadios_02.getProgramName());
                    ((ViewHolderOthers) holder).tv_item_02_playcount_id.setText(topRadios_02.getPlayCount()+"");

                    TopRadio topRadios_03 = topRadios.get(2);
                    Picasso.with(context).load(topRadios_03.getCoverLarge()).into(((ViewHolderOthers) holder).iv_peritem_03);
                    ((ViewHolderOthers) holder).tv_item_03_radioname.setText(topRadios_03.getName());
                    ((ViewHolderOthers) holder).tv_item_03_programname_id.setText(topRadios_03.getProgramName());
                    ((ViewHolderOthers) holder).tv_item_03_playcount_id.setText(topRadios_03.getPlayCount()+"");
                }
                break;
            case TYPE_OTHERS_03:
                if (holder instanceof ViewHolderOthers) {
                    TextView tv_live_peritem_head = ((ViewHolderOthers) holder).tv_live_peritem_head;
                    tv_live_peritem_head.setText("收听历史");

                }
                break;
        }
    }






    @Override
    public int getItemCount() {
        return 4;
    }



    //第一个布局的vh
    public class ViewHolderFirst extends RecyclerView.ViewHolder{

        private final TextView localRadio;
        private final TextView countryRadio;
        private final TextView provinceRadio;
        private final TextView internetRadio;
        private final GridLayout gridLayoutRadios;

        public ViewHolderFirst(View itemView) {
            super(itemView);
            localRadio = (TextView) itemView.findViewById(R.id.tv_radio_local_id);
            countryRadio = (TextView) itemView.findViewById(R.id.tv_radio_country_id);
            provinceRadio = (TextView) itemView.findViewById(R.id.tv_radio_province_id);
            internetRadio = (TextView) itemView.findViewById(R.id.tv_radio_internet_id);

            gridLayoutRadios = ((GridLayout) itemView.findViewById(R.id.gridlayout_radios_id));

        }
    }
    //第二个布局的VH
    public class ViewHolderOthers extends RecyclerView.ViewHolder{

        private final ImageView iv_peritem_01;
        private final TextView tv_item_01_radioname;
        private final TextView tv_item_01_programname_id;
        private final TextView tv_item_01_playcount_id;

        private final ImageView iv_peritem_02;
        private final TextView tv_item_02_radioname;
        private final TextView tv_item_02_programname_id;
        private final TextView tv_item_02_playcount_id;

        private final ImageView iv_peritem_03;
        private final TextView tv_item_03_radioname;
        private final TextView tv_item_03_programname_id;
        private final TextView tv_item_03_playcount_id;

        private final TextView tv_live_peritem_head;

        public ViewHolderOthers(View itemView) {
            super(itemView);
            tv_live_peritem_head = (TextView) itemView.findViewById(R.id.include_peritem_head).findViewById(R.id.tv_live_peritem_head);


            View item01 = itemView.findViewById(R.id.include_peritem_01);
            iv_peritem_01 = ((ImageView) item01.findViewById(R.id.iv_peritem_id));
            tv_item_01_radioname = ((TextView) item01.findViewById(R.id.tv_peritem_radioname_id));
            tv_item_01_programname_id = ((TextView) item01.findViewById(R.id.tv_peritem_programName_id));
            tv_item_01_playcount_id = ((TextView) item01.findViewById(R.id.tv_peritem_playCount_id));

            View item02 = itemView.findViewById(R.id.include_peritem_02);
            iv_peritem_02 = ((ImageView) item02.findViewById(R.id.iv_peritem_id));
            tv_item_02_radioname = ((TextView) item02.findViewById(R.id.tv_peritem_radioname_id));
            tv_item_02_programname_id = ((TextView) item02.findViewById(R.id.tv_peritem_programName_id));
            tv_item_02_playcount_id = ((TextView) item02.findViewById(R.id.tv_peritem_playCount_id));

            View item03 = itemView.findViewById(R.id.include_peritem_03);
            iv_peritem_03 = ((ImageView) item03.findViewById(R.id.iv_peritem_id));
            tv_item_03_radioname = ((TextView) item03.findViewById(R.id.tv_peritem_radioname_id));
            tv_item_03_programname_id = ((TextView) item03.findViewById(R.id.tv_peritem_programName_id));
            tv_item_03_playcount_id = ((TextView) item03.findViewById(R.id.tv_peritem_playCount_id));

        }
    }



    //点击事件处理
    @Override
    public void onClick(View view) {

        switch (((String) view.getTag())) {
            case "localRadio":
            case "countryRadio":
            case "internetRadio":
                Intent intent = new Intent(context, NormalLiveSecondPageActivity.class);
                intent.putExtra("title",((TextView) view).getText());
                intent.putExtra("tag",(String) view.getTag());
                context.startActivity(intent);
                activity.overridePendingTransition(R.anim.push_left_in,android.R.anim.fade_out);
                break;
        }



    }
}
